package com.example.farmeri.ui.news;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.app.AlertDialog;

import com.example.farmeri.R;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.presenter.message.IMessageSendContract;
import com.example.farmeri.presenter.message.MessageSendPresenter;
import com.example.farmeri.tools.TextFilterUtils;
import com.example.farmeri.ui.base.BaseActivity;
import com.example.farmeri.ui.login.LoginActivity;
import com.example.farmeri.ui.users.model.UserList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewsSendActivity extends BaseActivity<MessageSendPresenter> implements IMessageSendContract {

    @BindView(R.id.news_send_to)//接收人筛选
    Spinner spinner;
    @BindView(R.id.news_send_content)
    EditText content;
    @BindView(R.id.news_send_remark)
    EditText remark;
    @BindView(R.id.news_send_title)
    EditText title;
//    @BindView(R.id.news_send_type)
//    EditText type;
    AlertDialog dialog;

    private String toUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newssend);
        ButterKnife.bind(this);
        initToolBar(R.id.news_send_toolbar,"发送消息",true,true);
        init();
        dialog =  initBaseDialog(this,"提交成功");
    }

    private void init() {
        present = new MessageSendPresenter(this);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                toUserId = idList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinnerAddData();
    }

    List<String> nickNameList = new ArrayList<>();
    List<String> idList = new ArrayList<>();

    ArrayAdapter spinnerAdapter;
    private void spinnerAddData() {
        getRole();
        spinnerAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
    }

    private void getRole() {
        present.getUsers(1,999);
    }

    public void submit(View view) {
        SharedPreferences sp = getSharedPreferences(getResources().getString(R.string.share_preference_user_info),MODE_PRIVATE);
        String id = sp.getString(getResources().getString(R.string.user_info_key_id),"");
        if (id.equals("")||id==null){
            showToast(this,"发送用户信息获取失败");
            return;
        }

        if (!isIllegalText()){
            present.reduceCredit(id);
        }

        Map<String ,String > map = new HashMap<>();
        map.put("content",content.getText().toString());
        map.put("messageType","用户消息类型");
        map.put("remarks",remark.getText().toString());
        map.put("title",title.getText().toString());
        map.put("toUser", toUserId);
        map.put("createId",id);
        present.sendMessage(map);
    }

    private boolean isIllegalText() {
        if (!TextFilterUtils.filterIllegalText(title.getText().toString(),1,15)){
            showToast("消息标题非法");
            return false;
        }
        if (!TextFilterUtils.filterIllegalText(content.getText().toString(),0,150)){
            showToast("消息内容非法");
            return false;
        }
        if (!TextFilterUtils.filterIllegalText(remark.getText().toString(),0,50)){
            showToast("消息备注非法");
            return false;
        }
        return true;
    }


    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void onUserInfoSuccess(UserList data) {
        idList.clear();
        nickNameList.clear();
        data.getUserList().forEach(userListBean -> {
            idList.add(userListBean.getId());
            nickNameList.add(userListBean.getNick_name());
        });
        spinnerAdapter.clear();
        spinnerAdapter.addAll(nickNameList);
    }

    @Override
    public void onUserInfoError(String msg) {
        showToast(msg);
    }

    @Override
    public void onSendMsgSuccess() {
        dialog.show();
    }

    @Override
    public void onSendMsgError(String msg) {
        showToast(msg);
    }

    @Override
    public void onCreditReduceSuccess() {
        if (DataParam.getUserInfo().getCredit()<=0){
            showToast("用户信誉值过低，已被锁定");
            startActivity(new Intent(this, LoginActivity.class));
        }
    }
}
